package com.taptech.doufu.ui.view;

import android.app.Activity;
import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import android.util.AttributeSet;
import android.view.MotionEvent;


import com.taptech.doufu.util.ScreenUtil2;
import com.taptech.doufu.ui.adapter.ImageSubjectAdapter;
import com.taptech.doufu.bean.AlbumsBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lsy on 2015/8/31.
 */

/**
 * 显示专题图及专题说明 每项一张图配一行字
 * 当图片为一张或两张时均分显示，当图片超过两张时，作为可以横向滑动的滚动条
 */
public class ImageSubjectViewGroup extends RecyclerView
{

    Context mContext;

    List<AlbumsBean> mDataList;
    ImageSubjectAdapter adapter;


    int mSubjectHeight = 0;
    boolean isTouchToParent;
    // 滑动坐标
    float oldX = 0;
    float oldY = 0;
    float xScrollSpeed = 0;
    float yScrollSpeed = 0;



    public ImageSubjectViewGroup(Context context)
    {

        super(context);
        initView(context);
    }

    public ImageSubjectViewGroup(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        initView(context);
    }

    public ImageSubjectViewGroup(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        initView(context);
    }

    private void initView(Context context)
    {
        // 数据初始化
        mContext = context;
        mDataList = new ArrayList<AlbumsBean>();
        adapter = new ImageSubjectAdapter(context, mDataList);
        LayoutManager layoutManager = new StaggeredGridLayoutManager(1, HORIZONTAL);
        this.setLayoutManager(layoutManager);
        this.setAdapter(adapter);
    }


    @Override
    protected void onMeasure(int widthSpec, int heightSpec)
    {
        widthSpec = MeasureSpec.makeMeasureSpec(ScreenUtil2.getScreenWidthPixel((Activity) mContext), MeasureSpec.AT_MOST);
        heightSpec = MeasureSpec.makeMeasureSpec(mSubjectHeight, MeasureSpec.EXACTLY);
        super.onMeasure(widthSpec, heightSpec);
    }

    public void setSubjectData(List<AlbumsBean> dataList)
    {
        if (mDataList != null && dataList != null && dataList.size() > 0)
        {
            mDataList.clear();
            mDataList.addAll(dataList);
            switch (dataList.size())
            {
                case 1:
                    if (dataList.get(0) != null && dataList.get(0).isNovelSingel())
                    {
                        mSubjectHeight = (int) ((ScreenUtil2.getScreenWidthPixel((Activity) mContext) - ScreenUtil2.dip2px(16)) / 3.0 + ScreenUtil2.dip2px(8));
                    }/* else if (dataList.get(0) != null && dataList.get(0).isTagSingle())
                    {
                        mSubjectHeight = (int) ((ScreenUtil2.getScreenWidthPixel((Activity) mContext) - ScreenUtil2.dip2px(16)) / 4 + ScreenUtil2.dip2px(4));
                    } */ else
                    {
                        mSubjectHeight = (int) ((ScreenUtil2.getScreenWidthPixel((Activity) mContext) - ScreenUtil2.dip2px(16)) / 2.29 + ScreenUtil2.dip2px(8));
                    }
                    isTouchToParent = false;

                    break;
                case 2:
                    mSubjectHeight = (int) ((ScreenUtil2.getScreenWidthPixel((Activity) mContext) - ScreenUtil2.dip2px(48)) / 3.5 + ScreenUtil2.dip2px(30));
                    isTouchToParent = false;
                    break;
                case 3:
                    mSubjectHeight = (int) ((ScreenUtil2.getScreenWidthPixel((Activity) mContext) - ScreenUtil2.dip2px(48)) / 3.0 + ScreenUtil2.dip2px(48));
                    isTouchToParent = false;
                    break;
                default:
                    mSubjectHeight = (int) ((ScreenUtil2.getScreenWidthPixel((Activity) mContext) - ScreenUtil2.dip2px(48)) / 3.5 + ScreenUtil2.dip2px(39));
                    isTouchToParent = true;
                    break;
            }
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev)
    {
        if (isTouchToParent)
        {
            // 可滑动专题栏，向上寻找容器，当他找到Listview上面的容器时，阻断触摸事件
            getParent().getParent().getParent().getParent().requestDisallowInterceptTouchEvent(true);
        }
        return super.dispatchTouchEvent(ev);
    }

    public void setImageUrls(List<String> imageUrls, List<String> imageTitle)
    {
        if (imageUrls == null)
        {
            return;
        }
        if (mDataList == null)
        {
            mDataList = new ArrayList<AlbumsBean>();
        } else
        {
            mDataList.clear();
        }
        for (int i = 0; i < imageUrls.size(); i++)
        {
            AlbumsBean bean = new AlbumsBean();
            bean.setIcon(imageUrls.get(i));
            if (imageTitle != null && imageTitle.size() > 0)
            {
                bean.setName(imageTitle.get(i));
            }
            mDataList.add(bean);
        }
        adapter.notifyDataSetChanged();
    }

    public List<AlbumsBean> getDataList()
    {
        return mDataList;
    }

    boolean isMoving = false;  //控件是否在滑动
    // 关闭滑动事件，但是不能关闭点击事件


}
